<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0)
        }
    </style>
</head>

<body>

</body>
<script type="text/javascript" src="hmac-sha1.js"></script>
<script>
    var signature = createSignature({
        appid: '4744',
        secretkey: 'ba04500d2b145eabfae3145785932c88',
        apiname: '食监东城',
        e: 600
    });
    console.log(signature);

    var url = "https://iauth-sandbox.wecity.qq.com/new/cgi-bin/auth.php"; //TODO
    var args = {
        "appid": '4744',
        "signature": signature,
        "redirect": "",
        "uid": "137",
        "type": 0
    };

    post(url, args);


    function createSignature(params) {
        if (params === undefined) params = {};
        let appid = params.appid || '';
        let secretkey = params.secretkey || '';
        let apiname = params.apiname || '';
        let e = params.e || 600;
        let time = (Date.parse(new Date()) / 1000).toString();
        let orignal = 'a=' + appid + '&m=' + apiname + '&t=' + time + '&e=' + e;

        let signature = CryptoJS.HmacSHA1(orignal, secretkey);
        var binaryData = hex2a(signature);

        var resDaa = binaryData.concat(orignal);

        var base64 = btoa(resDaa);
        return base64;

    }

    function hex2a(hexx) {
        var hex = hexx.toString();
        var str = '';
        for (var i = 0; i < hex.length; i += 2)
            str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
        return str;
    }

    function post(url, params) {
        var temp = document.createElement("form");
        temp.action = url;
        temp.method = "post";
        temp.style.display = "none";
        for (var x in params) {
            var opt = document.createElement("textarea");
            opt.name = x;
            opt.value = params[x];
            temp.appendChild(opt);
        }
        document.body.appendChild(temp);
        temp.submit();
        return temp;
    }
</script>

</html>